var t = getApp().api,
  a = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: {},
    showModel: 0,
    input_text: '',
    input_type: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    wx.setNavigationBarTitle({
      title: '个人资料'
    })
    getApp().page.onLoad(this, t);
    this.getData(t);
  },
  bindKeyInput(e) {
    this.setData({
      input_text: e.detail.value
    });
  },
  editDesc(e) {
    console.log(e.currentTarget.dataset.type);
    this.setData({
      type: e.currentTarget.dataset.type,
      showModel: 1,
      input_text: ''
    });
  },
  hideModal() {
    this.setData({
      type: 0,
      showModel: 0,
      input_text: ''
    });
  },
  confirmInput() {
    var s = this,
      input_txt = s.data.input_text,
      user = this.data.user_info;
    if (!input_txt) {
      getApp().core.showModal({
        title: "提示",
        content: "请输入内容",
        showCancel: !1
      });
      return;
    }
    if (this.data.type == 1) {
      user.nickname = input_txt;
    } else if (this.data.type == 2) {
      user.desc = input_txt;
    }
    s.upUser(user);
  },
  getData(t) {
    var a = this;
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.user_center,
      data: {},
      success: function (t) {
        0 == t.code && (a.data.user_info = t.data.user, a.setData({
          user_info: a.data.user_info
        }));
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  bindRegionChange(e) {
    var user = this.data.user_info;
    if (!e.detail.value) {
      getApp().core.showModal({
        title: "提示",
        content: "请选择地址",
        showCancel: !1
      });
      return;
    }
    getApp().core.setStorageSync(getApp().const.USERREGION, e.detail.value[1]),
    user.province_name = e.detail.value[0];
    user.city_name = e.detail.value[1];
    user.district_name = e.detail.value[2];
    this.upUser(user);
  },
  gain() {
    var that = this;
    wx.showActionSheet({
      itemList: ['从手机相册选择', '拍照'],
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }

      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  }, // 图片本地路径
  chooseWxImage: function (type) {
    var that = this;
    var imgsPaths = that.data.imgs;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        that.upImgs(res.tempFilePaths[0], 0) //调用上传方法
      }
    })
  },
  upImgs(img, index) {
    var that = this,
      user = that.data.user_info;
    wx.uploadFile({
      url: t.default.upload_image,
      name: "image",
      formData: {},
      header: {
        'content-type': 'multipart/form-data'
      },
      filePath: img,
      complete: function (t) {
        if (t.data) {
          var s = JSON.parse(t.data);
          if (0 == s.code) {
            user.head = s.data.url;
            that.upUser(user);
          }
        }
      }
    });
  },
  upUser(user) {
    var a = this;
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.edit_user_center,
      method: "POST",
      data: user,
      success: function (t) {
        0 == t.code && (a.setData({
          user_info: user,
          type: 0,
          input_text: '',
          showModel: 0
        }));
        console.log(a.data.user_info);
        getApp().core.showToast({
          title: t.msg,
          icon: 'none'
        });
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url,s=this;
    if (!gopage) {
      return;
    }
    wx.navigateTo({
      url: gopage,
      events: {
        someEvent: function(data) {
          s.upUser(data)
        }
      },
      success: function(res) {
        res.eventChannel.emit('sendData', s.data.user_info)
      }
    })
  },
})